package me.pearjelly.pmc.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import me.pearjelly.pmc.model.PicGroupDoc;
import me.pearjelly.pmc.service.SimpleSearchService;
import me.pearjelly.pmc.util.Logger;
import me.pearjelly.pmc.util.Pagination;

/**
 * @author xiaobinghan 
 */
public class SimpleSearchAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4996355217476013253L;

	public String search() {
		HttpServletRequest request = getRequest();
		this.domain = request.getHeader("Host");
		this.channel = channelMap.get(domain);
		if(this.channel == null){
			this.channel = "default";
		}
		long s = System.currentTimeMillis();
		this.result = simpleSearchService.pageSearch(this.item, this.cate,
				this.page);
		long e = System.currentTimeMillis();
		this.searchTime = e -s;
		log.info("Search total:%d, return:%d, spends %d ms.", this.page.getRecordSum(), this.result.size(), this.searchTime);
		return SUCCESS;
	}

	public String getPicResult() {		
		return SUCCESS;
	}

	private static final Logger log = new Logger();

	private SimpleSearchService simpleSearchService;
	private Map<String,String> channelMap;
	public void setSimpleSearchService(SimpleSearchService simpleSearchService) {
		this.simpleSearchService = simpleSearchService;
	}
	public void setChannelMap(Map<String, String> channelMap) {
		this.channelMap = channelMap;
	}

	private Pagination page = new Pagination(1, 20);

	private long searchTime;
	private String item;
	private String cate;
	private String domain;
	private String channel;	
	private List<PicGroupDoc> result;

	public long getSearchTime() {
		return searchTime;
	}

	public void setItem(String item) {
		this.item = item != null ? item.trim() : null;
	}

	public void setCate(String cate) {
		this.cate = cate != null ? cate.trim() : null;
	}

	public void setPage(Pagination page) {
		this.page = page;
	}

	public Pagination getPage() {
		return page;
	}

	public String getItem() {
		return item;
	}

	public String getCate() {
		return cate;
	}

	public String getDomain() {
		return domain;
	}

	public String getChannel() {
		return channel;
	}

	public List<PicGroupDoc> getResult() {
		return result;
	}
}
